package com.suncaper.han_yi_system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suncaper.han_yi_system.pojo.dto.ClothingStyleDto;
import com.suncaper.han_yi_system.pojo.dto.ClothingStyleQueryDto;
import com.suncaper.han_yi_system.pojo.entity.ClothingStyle;
import com.suncaper.han_yi_system.pojo.entity.SizeRecommendation;
import com.suncaper.han_yi_system.pojo.vo.ClothingStyleVo;
import com.suncaper.han_yi_system.result.Result;
import com.suncaper.han_yi_system.service.ClothingStyleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/clothingStyle")
@CrossOrigin(origins = "*")
@Tag(name = "寒衣管理")
@Slf4j
public class ClothingStyleController {

    @Autowired
    ClothingStyleService clothingStyleService;

    @Operation(summary = "新增衣服类型")
    @PostMapping("/save")
    public Result save(@RequestBody ClothingStyleDto clothingStyleDto){
        clothingStyleService.saveWithSize(clothingStyleDto);
        return Result.ok();
    }

    @Operation(summary = "更新衣服类型")
    @PutMapping("/update")
    public Result update(@RequestBody ClothingStyleDto clothingStyleDto){
        clothingStyleService.updateWithSize(clothingStyleDto);
        return Result.ok();
    }

    @Operation(summary = "删除衣服类型")
    @DeleteMapping("/delete")
    public Result delete(Long id){
        if(Objects.isNull(id)){
            return Result.error().message("id不能为空");
        }
        clothingStyleService.removeWithSize(id);
        return Result.ok();
    }

    @Operation(summary = "根据id获取衣服类型")
    @GetMapping("/getById")
    public Result getById(Long id){
        if(Objects.isNull(id)){
            return Result.error().message("id不能为空");
        }

        ClothingStyle cs = clothingStyleService.getById(id);
        List<SizeRecommendation> sizes = clothingStyleService.getByStyleId(id);

        ClothingStyleVo clothingStyleVo = new ClothingStyleVo();

        BeanUtils.copyProperties(cs, clothingStyleVo);
        clothingStyleVo.setSizes(sizes);

        return Result.ok().data("clothingStyle", clothingStyleVo);
    }

    @Operation(summary = "获取衣服类型列表")
    @GetMapping("/page")
    public Result pageList(ClothingStyleQueryDto clothingStyleDto){
        Page<ClothingStyleVo> list = clothingStyleService.pageList(clothingStyleDto);
        return Result.ok().data("records", list.getRecords()).data("total", list.getTotal());
    }

    @Operation(summary = "根据批次id获取衣服类型列表")
    @PostMapping("/getListByBatchId")
    public Result getListByBatchId(@RequestBody Long batchId){
        List<ClothingStyle> clothingStyleList = clothingStyleService.getClothingStyleList(batchId);
        return Result.ok().data("clothingStyleList", clothingStyleList);
    }

}
